package com.study.talk.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.SecurityUtil;
import com.study.talk.module.entity.Friend;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.entity.vo.FriendVo;
import com.study.talk.module.entity.vo.GroupUserInfoVo;
import com.study.talk.module.service.FriendService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Resource
    private FriendService friendService;

    @ApiOperation("获取所有好友")
    @GetMapping("/friendList/{id}")
    public FwResult<List<Friend>> getFriendList(@PathVariable("id") Integer id) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        List<Friend> friendList = friendService.list(wrapper);
        return FwResult.ok(friendList, "查询成功");
    }

    @ApiOperation("获取所有好友")
    @GetMapping("/friendList")
    public FwResult<List<Friend>> getCurrentFriends() {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", SecurityUtil.getUser().getId());
        List<Friend> friendList = friendService.list(wrapper);
        return FwResult.ok(friendList, "查询成功");
    }

    @ApiOperation("获取所有好友（详细信息）")
    @GetMapping("/userInfoList/{id}")
    public FwResult<List<UserInfo>> getFriendUserInfoList(@PathVariable("id") Integer id) {
        return FwResult.ok(friendService.getUserInfoList(id), "查询成功");
    }

    @ApiOperation("获取所有好友（详细信息）")
    @GetMapping("/userInfoList")
    public FwResult<List<UserInfo>> getCurrentFriendUserInfos() {
        return FwResult.ok(friendService.getUserInfoList(SecurityUtil.getUser().getId()), "查询成功");
    }

    @ApiOperation("查询是否为好友")
    @PostMapping("/isFriend")
    public FwResult<Boolean> queryIsFriend(@RequestBody FriendVo friendVo) {
        return FwResult.ok(friendService.queryIsFriend(friendVo.getUserId(), friendVo.getFriendId()));
    }

    @ApiOperation("查询是否为好友")
    @GetMapping("/isFriend/{user_id}")
    public FwResult<Boolean> queryIsFriend(@PathVariable("user_id") Integer userId) {
        return FwResult.ok(friendService.queryIsFriend(SecurityUtil.getUser().getId(), userId));
    }

    @ApiOperation("获取不在指定群聊中的好友")
    @PostMapping("/get/not/in/group/list")
    public FwResult<List<UserInfo>> queryIsFriend(@RequestBody GroupUserInfoVo groupUserInfoVo) {
        return FwResult.ok(friendService.getUserInfoListNotInGroup(groupUserInfoVo.getGroupId(), groupUserInfoVo.getUserId()));
    }

    @ApiOperation("删除好友")
    @DeleteMapping("/delete/{friendId}")
    public FwResult<Boolean> deleteFriend(@PathVariable("friendId") Integer friendId) {
        Integer userId = SecurityUtil.getUser().getId();
        friendService.removeFriend(userId, friendId);
        friendService.removeFriend(friendId, userId); //互相删除
        return FwResult.ok(true);
    }

}
